package org.jeecg.common.modules.doctor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.jeecg.common.modules.doctor.entity.Doctor;
import org.jeecg.common.modules.doctor.entity.DoctorRegister;
import org.jeecg.common.modules.doctor.entity.DoctorRegisterRecord;
import org.jeecg.common.modules.doctor.mapper.DoctorRegisterMapper;
import org.jeecg.common.modules.doctor.service.IDoctorRegisterRecordService;
import org.jeecg.common.modules.doctor.service.IDoctorRegisterService;
import org.jeecg.common.modules.doctor.vo.DoctorRegisterKeyVo;
import org.jeecg.common.modules.doctor.vo.DoctorRegisterParamVo;
import org.jeecg.common.modules.doctor.vo.DoctorRegisterPartVo;
import org.jeecg.common.modules.doctor.vo.DoctorRegisterVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: doctor_register
 * @Author: jeecg-boot
 * @Date:   2020-12-18
 * @Version: V1.0
 */
@Slf4j
@Service
public class DoctorRegisterServiceImpl extends ServiceImpl<DoctorRegisterMapper, DoctorRegister> implements IDoctorRegisterService {
    @Resource
    private DoctorRegisterMapper doctorRegisterMapper;
    @Resource
    private IDoctorRegisterRecordService doctorRegisterRecordService;

    @Override
    public int checkRegister(String doctorId){
        //我要获取当前的日期
        Date date = new Date();
        //设置要获取到什么样的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //获取String类型的时间
        String visitTime = sdf.format(date);
        log.info("时间字符串：{}",visitTime);
        LambdaQueryWrapper<DoctorRegister> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DoctorRegister::getDoctorId,doctorId);
        queryWrapper.ge(DoctorRegister::getVisitTime,visitTime);
        return count(queryWrapper);
    }
    @Override
    public List<DoctorRegisterVo> getInfoList(String doctorName,String hospitalId,String departmentId,String visitTime, Integer dataStart, Integer pageSize){
        return doctorRegisterMapper.selectDoctorRegisterInfo(doctorName,hospitalId,departmentId,visitTime,dataStart,pageSize);
    }

    @Override
    public Integer getDoctorRegisterCount(String doctorName,String hospitalId,String departmentId,String visitTime){
        return doctorRegisterMapper.getDoctorRegisterCount(doctorName,hospitalId,departmentId,visitTime);
    }

    @Override
    public DoctorRegister getDoctorRegisterByDoctorIdAndVisitTimePart(String doctorId,String visitTime,String visitPart){
        LambdaQueryWrapper<DoctorRegister> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DoctorRegister::getDoctorId,doctorId);
        queryWrapper.eq(DoctorRegister::getVisitTime,visitTime);
        queryWrapper.eq(DoctorRegister::getVisitPart,visitPart);
        queryWrapper.eq(DoctorRegister::getInvalid,0);
        return getOne(queryWrapper);
    }

    @Override
    public List<DoctorRegisterKeyVo> getVisitTime(String doctorId,String nowDate){
        return doctorRegisterMapper.getVisitTime(doctorId,nowDate);
    }

    @Override
    public List<DoctorRegisterParamVo> getRegisterVisitPart(String visitTime) throws ParseException {
        LambdaQueryWrapper<DoctorRegister> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DoctorRegister::getVisitTime,visitTime);
        List<DoctorRegister> doctorRegisters = list(queryWrapper);
        List<DoctorRegisterParamVo> doctorRegisterParamList = new ArrayList<>();

        int allNum = 0;
        int alreadyNum = 0;
        String status = null;
        String dateStrNow = getNowDate(); //当天的日期字符串

        //初始化一维三个元素的list
        for(int key = 0;key < 3;key++){
            DoctorRegisterParamVo doctorRegisterParamVo = new DoctorRegisterParamVo();
            doctorRegisterParamList.add(doctorRegisterParamVo);
        }
        log.info("初始化的数据：{}",doctorRegisterParamList);
        int tarKey = 0; //目标key
        for(int key = 0;key < doctorRegisters.size();key++){
            if(doctorRegisters.get(key).getVisitTime().equals(dateStrNow)){ //当天的挂号
                Date dateZhong = strToDate(getNowDate() + " 12:00:00");  //当天中午的日期字符串
                Date dateXia   = strToDate(getNowDate() + " 18:00:00");  //当天下午的日期字符串

                //当天上午不允许挂号，所以只比较当前时间与中午时间、下午时间
                Date now = new Date();
                if(now.before(dateZhong)){ // 展示下午或晚上的挂号
                    if(doctorRegisters.get(key).getVisitPart().equals("下午")){
                        tarKey = 1;
                    }else if(doctorRegisters.get(key).getVisitPart().equals("晚上")){
                        tarKey = 2;
                    }else{
                        tarKey = 0;
                    }

                    if(tarKey != 0){
                        allNum = doctorRegisters.get(key).getCount(); //挂号量
                        alreadyNum = doctorRegisterRecordService.getRecordCount(doctorRegisters.get(key).getId());  //已经挂号的量
                        if(allNum == alreadyNum){ status = "forbid";  //禁止挂号
                        }else{ status = "allow"; }
                        doctorRegisterParamList.get(tarKey).setId(doctorRegisters.get(key).getId());          //挂号id
                        doctorRegisterParamList.get(tarKey).setPart(doctorRegisters.get(key).getVisitPart()); //挂号的时间段：上午、下午、晚上
                        doctorRegisterParamList.get(tarKey).setAllNum(doctorRegisters.get(key).getCount());   //总挂号量
                        doctorRegisterParamList.get(tarKey).setAlreadyNum(alreadyNum);
                        doctorRegisterParamList.get(tarKey).setSurplusNum(allNum - alreadyNum);
                        doctorRegisterParamList.get(tarKey).setStatus(status);
                    }
                }else if(now.before(dateXia) && doctorRegisters.get(key).getVisitPart().equals("晚上")){
                    tarKey = 2; // 只展示晚上的挂号信息
                    allNum = doctorRegisters.get(key).getCount(); //挂号量
                    alreadyNum = doctorRegisterRecordService.getRecordCount(doctorRegisters.get(key).getId());  //已经挂号的量
                    if(allNum == alreadyNum){ status = "forbid";  //禁止挂号
                    }else{ status = "allow"; }
                    doctorRegisterParamList.get(tarKey).setId(doctorRegisters.get(key).getId());          //挂号id
                    doctorRegisterParamList.get(tarKey).setPart(doctorRegisters.get(key).getVisitPart()); //挂号的时间段：上午、下午、晚上
                    doctorRegisterParamList.get(tarKey).setAllNum(doctorRegisters.get(key).getCount());   //总挂号量
                    doctorRegisterParamList.get(tarKey).setAlreadyNum(alreadyNum);
                    doctorRegisterParamList.get(tarKey).setSurplusNum(allNum - alreadyNum);
                    doctorRegisterParamList.get(tarKey).setStatus(status);
                }
            }else{
                if(doctorRegisters.get(key).getVisitPart().equals("上午")){
                    tarKey = 0;
                }else if(doctorRegisters.get(key).getVisitPart().equals("下午")){
                    tarKey = 1;
                }else if(doctorRegisters.get(key).getVisitPart().equals("晚上")){
                    tarKey = 2;
                }

                allNum = doctorRegisters.get(key).getCount(); //挂号量
                alreadyNum = doctorRegisterRecordService.getRecordCount(doctorRegisters.get(key).getId());  //已经挂号的量
                if(allNum == alreadyNum){ status = "forbid";  //禁止挂号
                }else{ status = "allow"; }
                doctorRegisterParamList.get(tarKey).setId(doctorRegisters.get(key).getId());          //挂号id
                doctorRegisterParamList.get(tarKey).setPart(doctorRegisters.get(key).getVisitPart()); //挂号的时间段：上午、下午、晚上
                doctorRegisterParamList.get(tarKey).setAllNum(doctorRegisters.get(key).getCount());   //总挂号量
                doctorRegisterParamList.get(tarKey).setAlreadyNum(alreadyNum);
                doctorRegisterParamList.get(tarKey).setSurplusNum(allNum - alreadyNum);
                doctorRegisterParamList.get(tarKey).setStatus(status);
            }
        }
        return doctorRegisterParamList;
    }
    @Override
    public DoctorRegisterPartVo getRegisterPart(String doctorRegisterId){
        return doctorRegisterMapper.getRegisterPart(doctorRegisterId);
    }

    /**
     * 获取当前的日期字符串
     * @return
     */
    public static String getNowDate() {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(date);
    }

    /**
     * 字符串转日期
     */
    public static Date strToDate(String str) throws ParseException {
        Date date = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.parse(str);
    }
    /**
     * 获取出诊时间
     * @param vo
     * @return
     */
    @Override
    public  DoctorRegisterVo queryDoctorsVisitTime(DoctorRegisterVo vo) {
        return doctorRegisterMapper.queryDoctorsVisitTime(vo);
    }
    /**
     * 挂号医院是否有号源
     * @param doctorId
     * @return
     */
    @Override
    public List<DoctorRegisterVo> getDoctorRegisterHospital(String doctorId, String startDate, String endDate,String registerHospitalId){
        return doctorRegisterMapper.getDoctorRegisterHospital(doctorId, startDate, endDate, registerHospitalId);
    }
    /**
     * 查询是否有号
     * @param vo
     * @return
     */
    @Override
    public  List<DoctorRegisterVo> queryOrderIsHave(DoctorRegisterVo vo) {
        return doctorRegisterMapper.queryOrderIsHave(vo);
    }
    /**
     * 查询是否有号
     * @param vo
     * @return
     */
    @Override
    public  List<DoctorRegisterVo> queryOrderIsHaveNew(DoctorRegisterVo vo) {
        return doctorRegisterMapper.queryOrderIsHaveNew(vo);
    }
}
